package cn.fch.gotrip.controller;

import cn.fch.gotrip.api.BeanResponse;
import cn.fch.gotrip.entity.Admin;
import cn.fch.gotrip.service.AdminService;
import cn.fch.gotrip.util.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: gotrip
 * @description:
 * @CreatedBy: fch
 * @create: 2023-01-14 17:10
 **/
@RestController
@RequestMapping("admin")
public class AdminController {

    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    AdminService adminService;

    @PostMapping("login")
    public BeanResponse login(@RequestBody Admin admin,
                              HttpServletResponse response){
        if (!StringUtils.hasText(admin.getUsername()) || !StringUtils.hasText(admin.getPassword()))
            return BeanResponse.badRequest("请输入完整信息");
        return adminService.login(admin, response);
    }

    @GetMapping("info")
    public BeanResponse info(HttpServletRequest request){
        String token = request.getHeader("token");
        return adminService.getAdminInfoByToken(token);
    }

    @DeleteMapping("logout")
    public BeanResponse logout(){
        return  BeanResponse.ok(null);
    }
}
